package com.app.view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class MyLetterListView extends View 
{
	OnTouchingLetterChangedListener onTouchingLetterChangedListener;
	
	String[] b = { "定位","最近","热门","#", "A", "B", "C", 
			"D", "E", "F", "G", "H", "I", "J", "K",
			"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
			"Y", "Z" };
	int choose = -1;
	Paint paint = new Paint();
	boolean showBkg = false;
	// 展示选中的字母
    private TextView showChooseText;
	float textSize  = 12f;
	
	public MyLetterListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public MyLetterListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		textSize= TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, textSize, getResources().getDisplayMetrics());
		
	}

	public MyLetterListView(Context context) {
		super(context);
		
	}

	/**
	 * 为MyLetterListView设置显示字母的TextView
	 * 
	 * @param showChooseText
	 *            the showChooseText to set
	 */
	public void setShowChooseText(TextView showChooseText) {
		this.showChooseText = showChooseText;
	}
	
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
//		if (showBkg) {
//			canvas.drawColor(Color.parseColor("#00000000"));
//		}
		// 获取焦点改变背景颜色
		int height = getHeight();
		int width = getWidth();
		int singleHeight = height / b.length;
		for (int i = 0; i < b.length; i++) {
			paint.setColor(Color.BLACK);
			paint.setTypeface(Typeface.DEFAULT_BOLD);
			paint.setAntiAlias(true);
			paint.setTextSize(textSize);
			// 选中状态
			if (i == choose) {
				paint.setColor(Color.BLUE);
				paint.setTextSize(textSize+3);
				paint.setFakeBoldText(true);
			}
			// x坐标等于中间-字符串宽度的一半.
			float xPos = width / 2 - paint.measureText(b[i]) / 2;
			float yPos = singleHeight * i + singleHeight;
			canvas.drawText(b[i], xPos, yPos, paint);
			paint.reset();// 重置画笔
		}
		

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		final int action = event.getAction();
		final float y = event.getY();
		final int oldChoose = choose;
		//final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
		//final int c = (int) (y / getHeight() * b.length);

		// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
		int c = 0;
		if (y >= 0 && y <= getHeight()) {
			c = (int) (y / getHeight() * b.length);
		} else if (y > getHeight()) {
			c = b.length - 1;
		}
		if (action == MotionEvent.ACTION_UP) {
			setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
			choose = -1;
			if (showChooseText != null) {
				showChooseText.setVisibility(View.INVISIBLE);
			}
		} else {
			setBackgroundDrawable(new ColorDrawable(Color.GRAY));
			setBackgroundColor(Color.GRAY);
			if (oldChoose != c) {
				if (c >= 0 && c < b.length) {
					if (onTouchingLetterChangedListener != null) {
						onTouchingLetterChangedListener.onTouchingLetterChanged(b[c],c);
					}
				}
				if (showChooseText != null) {
					showChooseText.setText(b[c]);
					showChooseText.setVisibility(View.VISIBLE);
				}
				choose = c;
			}
		}
		invalidate();
//		switch (action) {
//		case MotionEvent.ACTION_DOWN:
//			showBkg = true;
//			if (oldChoose != c && listener != null) {
//				if (c > 0 && c < b.length) {
//					listener.onTouchingLetterChanged(b[c]);
//					choose = c;
//					invalidate();
//				}
//			}
//
//			break;
//		case MotionEvent.ACTION_MOVE:
//			if (oldChoose != c && listener != null) {
//				if (c > 0 && c < b.length) {
//					listener.onTouchingLetterChanged(b[c]);
//					choose = c;
//					invalidate();
//				}
//			}
//			break;
//		case MotionEvent.ACTION_UP:
//			showBkg = false;
//			setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
//			if (showChooseText != null) {
//				showChooseText.setVisibility(View.INVISIBLE);
//			}
//			choose = -1;
//			invalidate();
//			break;
//		}
		return true;
	}

	public boolean onTouchEvent(MotionEvent event) {
		return super.onTouchEvent(event);
	}

	public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener) 
	{
		this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
	}

	public interface OnTouchingLetterChangedListener {
		public void onTouchingLetterChanged(String s, int position);
	}

}
